package hu.dynamicwebshop.contents;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import hu.dynamicwebshop.MyVaadinUI;
import hu.dynamicwebshop.daos.impls.AddressDAOImpl;
import hu.dynamicwebshop.daos.impls.CountryDAOImpl;
import hu.dynamicwebshop.daos.impls.ShopUserDAOImpl;
import hu.dynamicwebshop.entities.Address;
import hu.dynamicwebshop.entities.Country;
import hu.dynamicwebshop.entities.ShopUser;
import hu.dynamicwebshop.gui.ContentLayer;
import hu.dynamicwebshop.gui.PopupWindow;
import hu.dynamicwebshop.languagemanager.LanguageManager;
import hu.dynamicwebshop.shacoder.SHACoder;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * @author Szecskó Zoltán <zoltan.szecsko@gmail.com>
 */
public class RegistrationContent extends GridLayout {

    private static final long serialVersionUID = 1L;
    private int colNum = 2;
    private int rowNum = 20;
    private TextField usernameField;
    private PasswordField passwordField;
    private TextField emailField;
    private TextField fullnameField;
    private TextField phoneNumberField;
    private ComboBox homeCountryComboBox;
    private TextField homePostalCodeField;
    private TextField homeSettlementField;
    private TextField homePublicPremisesNameField;
    private TextField homeHouseNumberField;
    private TextField homeStoreyDoorField;
    private ComboBox deliveryCountryComboBox;
    private TextField deliveryPostalCodeField;
    private TextField deliverySettlementField;
    private TextField deliveryPublicPremisesNameField;
    private TextField deliveryHouseNumberField;
    private TextField deliveryStoreyDoorField;

    @Autowired
    private CountryDAOImpl cdi;
    @Autowired
    private AddressDAOImpl adi;
    @Autowired
    private ShopUserDAOImpl sudi;
    @Autowired
    private SHACoder shac;
    @Autowired
    private ContentLayer contentLayer;
    @Autowired
    private LanguageManager languageManager;
    private List<Country> countryList;

    public RegistrationContent() {
        setColumns(colNum);
        setRows(rowNum);
    }

    public void createRegistrationLayer() {
        final String countryCode = languageManager.getCountryCode();

        contentLayer.removeAllComponents();
        this.removeAllComponents();
        contentLayer.addComponent(this);

        addUsername(countryCode);
        addPassword(countryCode);
        addEmail(countryCode);
        addFullname(countryCode);
        addPhoneNumber(countryCode);

        Label homeAddressLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "addressInformationRegistration"));
        addComponent(homeAddressLabel, 0, 5);

        addHomeCountry(countryCode);
        addHomePostalCode(countryCode);
        addHomeSettlement(countryCode);
        addHomePublicPremisesName(countryCode);
        addHomeHouseNumber(countryCode);
        addHomeStoreyDoor(countryCode);

        Label deliveryAddressLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "deliveryAddressRegistration"));
        addComponent(deliveryAddressLabel, 0, 12);

        addDeliveryCountry(countryCode);
        addDeliveryPostalCode(countryCode);
        addDeliverySettlement(countryCode);
        addDeliveryPublicPremisesName(countryCode);
        addDeliveryHouseNumber(countryCode);
        addDeliveryStoreyDoor(countryCode);

        Button registrationButton = new Button(languageManager.getOutputTextMap().get(countryCode + "registrationButtonRegistration"));
        registrationButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                boolean homeCountryComboBoxIsEmpty = homeCountryComboBox.getValue() == null;
                boolean homePostalCodeIsEmpty = homePostalCodeField.getValue().toString().isEmpty();
                boolean homeSettlementIsEmpty = homeSettlementField.getValue().toString().isEmpty();
                boolean homePublicPremisesNameIsEmpty = homePublicPremisesNameField.getValue().toString().isEmpty();
                boolean homeHouseNumberIsEmpty = homeHouseNumberField.getValue().toString().isEmpty();

                boolean deliveryCountryComboBoxIsEmpty = deliveryCountryComboBox.getValue() == null;
                boolean deliveryPostalCodeIsEmpty = deliveryPostalCodeField.getValue().toString().isEmpty();
                boolean deliverySettlementIsEmpty = deliverySettlementField.getValue().toString().isEmpty();
                boolean deliveryPublicPremisesNameIsEmpty = deliveryPublicPremisesNameField.getValue().toString().isEmpty();
                boolean deliveryHouseNumberIsEmpty = deliveryHouseNumberField.getValue().toString().isEmpty();

                boolean usernameIsEmpty = usernameField.getValue().toString().isEmpty();
                boolean passwordIsEmpty = passwordField.getValue().toString().isEmpty();
                boolean emailIsEmpty = emailField.getValue().toString().isEmpty();
                boolean fullnameIsEmpty = fullnameField.getValue().toString().isEmpty();
                boolean phoneNumberIsEmpty = phoneNumberField.getValue().toString().isEmpty();

                if (homeCountryComboBoxIsEmpty || homePostalCodeIsEmpty || homeSettlementIsEmpty
                        || homePublicPremisesNameIsEmpty || homeHouseNumberIsEmpty
                        || deliveryCountryComboBoxIsEmpty || deliveryPostalCodeIsEmpty
                        || deliverySettlementIsEmpty || deliveryPublicPremisesNameIsEmpty
                        || deliveryHouseNumberIsEmpty || usernameIsEmpty || passwordIsEmpty
                        || emailIsEmpty || fullnameIsEmpty || phoneNumberIsEmpty) {
                    
                   MyVaadinUI.getCurrent().addWindow(new PopupWindow("üzenet", countryCode));
                } else {
                    try {
                        Address userHomeAddress = new Address();
                        Country homeCountry = cdi.findByName(homeCountryComboBox.getValue().toString());

                        userHomeAddress.setCountry(homeCountry);
                        userHomeAddress.setPostalCode(homePostalCodeField.getValue().trim());
                        userHomeAddress.setSettlement(homeSettlementField.getValue().trim());
                        userHomeAddress.setPublicPremisesName(homePublicPremisesNameField.getValue().trim());
                        userHomeAddress.setHouseNumber(homeHouseNumberField.getValue().trim());
                        userHomeAddress.setStoreyDoor(homeStoreyDoorField.getValue().trim());

                        adi.save(userHomeAddress);

                        Address userDeliveryAddress = new Address();
                        Country deliveryCountry = cdi.findByName(deliveryCountryComboBox.getValue().toString());

                        userDeliveryAddress.setCountry(deliveryCountry);
                        userDeliveryAddress.setPostalCode(deliveryPostalCodeField.getValue().trim());
                        userDeliveryAddress.setSettlement(deliverySettlementField.getValue().trim());
                        userDeliveryAddress.setPublicPremisesName(deliveryPublicPremisesNameField.getValue().trim());
                        userDeliveryAddress.setHouseNumber(deliveryHouseNumberField.getValue().trim());
                        userDeliveryAddress.setStoreyDoor(deliveryStoreyDoorField.getValue().trim());

                        adi.save(userDeliveryAddress);

                        ShopUser user = new ShopUser();
                        user.setUsername(usernameField.getValue().trim());
                        user.setPassword(shac.encode(passwordField.getValue().trim()));
                        user.setEmail(emailField.getValue().trim());
                        user.setFullName(fullnameField.getValue().trim());
                        user.setPhoneNumber(BigInteger.valueOf(Long.valueOf(phoneNumberField.getValue().trim())));
                        user.setHomeAddress(userHomeAddress);
                        user.setDeliveryAddress(userDeliveryAddress);

                        Timestamp currentTime = new Timestamp(new Date().getTime());
                        user.setRegistationDate(currentTime);

                        sudi.registration(user);

                    } catch (NumberFormatException ex) {

                    }
                }
            }
        });

        addComponent(registrationButton, 1, 19);
    }

    private final Comparator<Country> sortCountryByName = new Comparator<Country>() {
        @Override
        public int compare(Country o1, Country o2) {
            return ((Country) o1).getCountryName().compareTo(((Country) o2).getCountryName());
        }
    };

    public void sortCountryListByName(List<Country> countryList) {
        Collections.sort(countryList, sortCountryByName);
    }

    private void addUsername(String countryCode) {
        Label usernameLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "usernameRegistration"));
        addComponent(usernameLabel, 0, 0);
        usernameField = new TextField();
        addComponent(usernameField, 1, 0);
        usernameField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        usernameField.setRequiredError("Meg kell adnod a felhasználóneved!");
    }

    private void addPassword(String countryCode) {
        Label passwordLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "passwordRegistration"));
        addComponent(passwordLabel, 0, 1);
        passwordField = new PasswordField();
        addComponent(passwordField, 1, 1);
        passwordField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        passwordField.setRequiredError("Meg kell adnod a jelszavad!");
    }

    private void addEmail(String countryCode) {
        Label emailLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "mailAddressRegistration"));
        addComponent(emailLabel, 0, 2);
        emailField = new TextField();
        addComponent(emailField, 1, 2);
        emailField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        emailField.setRequiredError("Meg kell adnod az e-mail címed!");
    }

    private void addFullname(String countryCode) {
        Label fullnameLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "fullNameRegistration"));
        addComponent(fullnameLabel, 0, 3);
        fullnameField = new TextField();
        addComponent(fullnameField, 1, 3);
        fullnameField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        fullnameField.setRequiredError("Meg kell adnod a neved!");
    }

    private void addPhoneNumber(String countryCode) {
        Label phoneNumberLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "phoneNumberRegistration"));
        addComponent(phoneNumberLabel, 0, 4);
        phoneNumberField = new TextField();
        addComponent(phoneNumberField, 1, 4);
        phoneNumberField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        phoneNumberField.setRequiredError("Meg kell adnod a telefonszámod!");
    }

    private void addHomeCountry(String countryCode) {
        Label homeCountryLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "countryRegistration"));
        addComponent(homeCountryLabel, 0, 6);
        homeCountryComboBox = new ComboBox();

        if (countryList == null) {
            countryList = cdi.findAll();
        }

        sortCountryListByName(countryList);

        for (Country c : countryList) {
            homeCountryComboBox.addItem(c);
        }

        addComponent(homeCountryComboBox, 1, 6);
        homeCountryComboBox.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        homeCountryComboBox.setRequiredError("Ki kell választanod az országot az otthoni címedhez!");
    }

    private void addHomePostalCode(String countryCode) {
        Label homePostalCodeLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "postalCodeRegistration"));
        addComponent(homePostalCodeLabel, 0, 7);
        homePostalCodeField = new TextField();
        addComponent(homePostalCodeField, 1, 7);
        homePostalCodeField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        homePostalCodeField.setRequiredError("Meg kell adnod az irányítószámot az otthoni címedhez!");
    }

    private void addHomeSettlement(String countryCode) {
        Label homeSettlementLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "settlementRegistration"));
        addComponent(homeSettlementLabel, 0, 8);
        homeSettlementField = new TextField();
        addComponent(homeSettlementField, 1, 8);
        homeSettlementField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        homeSettlementField.setRequiredError("Meg kell adnod a település nevét az otthoni címedhez!");
    }

    private void addHomePublicPremisesName(String countryCode) {
        Label homePublicPremisesNameLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "publicPremisesNameRegistration"));
        addComponent(homePublicPremisesNameLabel, 0, 9);
        homePublicPremisesNameField = new TextField();
        addComponent(homePublicPremisesNameField, 1, 9);
        homePublicPremisesNameField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        homePublicPremisesNameField.setRequiredError("Meg kell adnod a közterület nevét az otthoni címedhez!");
    }

    private void addHomeHouseNumber(String countryCode) {
        Label homeHouseNumberLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "houseNumberRegistration"));
        addComponent(homeHouseNumberLabel, 0, 10);
        homeHouseNumberField = new TextField();
        addComponent(homeHouseNumberField, 1, 10);
        homeHouseNumberField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        homeHouseNumberField.setRequiredError("Meg kell adnod a házszámot az otthoni címedhez!");
    }

    private void addHomeStoreyDoor(String countryCode) {
        Label homeStoreyDoorLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "storeyDoorRegistration"));
        addComponent(homeStoreyDoorLabel, 0, 11);
        homeStoreyDoorField = new TextField();
        addComponent(homeStoreyDoorField, 1, 11);
    }

    private void addDeliveryCountry(String countryCode) {
        Label deliveryCountryLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "countryRegistration"));
        addComponent(deliveryCountryLabel, 0, 13);
        deliveryCountryComboBox = new ComboBox();

        for (Country c : countryList) {
            deliveryCountryComboBox.addItem(c);
        }

        addComponent(deliveryCountryComboBox, 1, 13);
        deliveryCountryComboBox.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        deliveryCountryComboBox.setRequiredError("Ki kell választanod az országot a szállítási címedhez!");
    }

    private void addDeliveryPostalCode(String countryCode) {
        Label deliveryPostalCodeLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "postalCodeRegistration"));
        addComponent(deliveryPostalCodeLabel, 0, 14);
        deliveryPostalCodeField = new TextField();
        addComponent(deliveryPostalCodeField, 1, 14);
        deliveryPostalCodeField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        deliveryPostalCodeField.setRequiredError("Meg kell adnod az irányítószámot a szállítási címedhez!");
    }

    private void addDeliverySettlement(String countryCode) {
        Label deliverySettlementLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "settlementRegistration"));
        addComponent(deliverySettlementLabel, 0, 15);
        deliverySettlementField = new TextField();
        addComponent(deliverySettlementField, 1, 15);
        deliverySettlementField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        deliverySettlementField.setRequiredError("Meg kell adnod a település nevét a szállítási címedhez!");
    }

    private void addDeliveryPublicPremisesName(String countryCode) {
        Label deliveryPublicPremisesNameLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "publicPremisesNameRegistration"));
        addComponent(deliveryPublicPremisesNameLabel, 0, 16);
        deliveryPublicPremisesNameField = new TextField();
        addComponent(deliveryPublicPremisesNameField, 1, 16);
        deliveryPublicPremisesNameField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        deliveryPublicPremisesNameField.setRequiredError("Meg kell adnod a közterület nevét a szállítási címedhez!");
    }

    private void addDeliveryHouseNumber(String countryCode) {
        Label deliveryHouseNumberLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "houseNumberRegistration"));
        addComponent(deliveryHouseNumberLabel, 0, 17);
        deliveryHouseNumberField = new TextField();
        addComponent(deliveryHouseNumberField, 1, 17);
        deliveryHouseNumberField.setRequired(true);
        //TODO adatbázisból szedje a szöveget
        deliveryHouseNumberField.setRequiredError("Meg kell adnod a házszámot a szállítási címedhez!");
    }

    private void addDeliveryStoreyDoor(String countryCode) {
        Label deliveryStoreyDoorLabel = new Label(languageManager.getOutputTextMap().get(countryCode + "storeyDoorRegistration"));
        addComponent(deliveryStoreyDoorLabel, 0, 18);
        deliveryStoreyDoorField = new TextField();
        addComponent(deliveryStoreyDoorField, 1, 18);
    }
}
